#include "frameprocess.h"

FrameProcess::FrameProcess()
{

}

QByteArray FrameProcess::addFrame(QByteArray data)
{
    QByteArray framHeader = "head";
    QByteArray frameTail = "tail";
    QByteArray array = framHeader + data + frameTail;
    return array;
}

QList<QByteArray> FrameProcess::getDatas(QByteArray data)
{
    QByteArray framHeader = "head";
    QByteArray frameTail = "tail";

    QList<QByteArray> extractedFrames;

    while (true) {
        int headerIndex = data.indexOf(framHeader);
        if (headerIndex == -1) {
            // No complete frame header found
            break;
        }

        int footerIndex = data.indexOf(frameTail, headerIndex + framHeader.size());
        if (footerIndex == -1) {
            // No complete frame footer found after header
            break;
        }

        // Extract the data between header and footer
        int dataStart = headerIndex + framHeader.size();
        QByteArray frameData = data.mid(dataStart, footerIndex - dataStart);
        extractedFrames.append(frameData);

        // Remove the processed frame from the buffer
        data = data.mid(footerIndex + frameTail.size());
    }

    return extractedFrames;
}
